package ua.org.nuos.sdms.middle.dao;

import org.apache.commons.lang.Validate;
import ua.org.nuos.sdms.middle.entity.UserToGroup;
import ua.org.nuos.sdms.middle.util.EntityValidator;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 * User: dio
 * Date: 8/24/12
 * Time: 3:32 PM
 * To change this template use File | Settings | File Templates.
 */
@Local
@Stateless
public class UserToGroupDaoBean {

    private static Logger logger = Logger.getLogger(UserToGroupDaoBean.class.getSimpleName());

    @PersistenceContext(unitName = "persistence")
    protected EntityManager em;

    public void create(UserToGroup userToGroup) {
        Validate.notNull(userToGroup);
        Validate.notNull(userToGroup.getUser());
        Validate.notNull(userToGroup.getGroup());
        Validate.notNull(userToGroup.getStatus());
        Validate.isTrue(userToGroup.getUser().getId() > 0);
        Validate.isTrue(userToGroup.getGroup().getId() > 0);

        em.persist(userToGroup);
    }

    public void update(UserToGroup userToGroup) {
        Validate.notNull(userToGroup);
        Validate.notNull(userToGroup.getUser());
        Validate.notNull(userToGroup.getGroup());
        Validate.notNull(userToGroup.getStatus());
        Validate.isTrue(userToGroup.getUser().getId() > 0);
        Validate.isTrue(userToGroup.getGroup().getId() > 0);

        UserToGroup old = find(userToGroup.getUser().getId(), userToGroup.getGroup().getId());
        EntityValidator.checkExisting(old,
                String.format("userId=%d, groupId=%d", userToGroup.getUser().getId(), userToGroup.getGroup().getId()));

        em.merge(userToGroup);
    }

    public UserToGroup find(long userId, long groupId) {
        Validate.isTrue(userId > 0);
        Validate.isTrue(groupId > 0);

        try {
            return em.createNamedQuery(UserToGroup.QUERY_FIND_BY_USER_ID_AND_GROUP_ID, UserToGroup.class)
                    .setParameter("userId", userId)
                    .setParameter("groupId", groupId)
                    .getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

    public List<UserToGroup> findByUser(long userId) {
        Validate.isTrue(userId > 0);

        try {
            return em.createNamedQuery(UserToGroup.QUERY_FIND_BY_USER_ID, UserToGroup.class)
                    .setParameter("userId", userId)
                    .getResultList();
        } catch (NoResultException e) {
            return new ArrayList<UserToGroup>();
        }
    }

    public List<UserToGroup> findByGroup(long groupId) {
        Validate.isTrue(groupId > 0);

        try {
            return em.createNamedQuery(UserToGroup.QUERY_FIND_BY_GROUP_ID, UserToGroup.class)
                    .setParameter("groupId", groupId)
                    .getResultList();
        } catch (NoResultException e) {
            return new ArrayList<UserToGroup>();
        }
    }

    public List<UserToGroup> findByGroupAndStatus(long groupId, UserToGroup.Status status) {
        Validate.isTrue(groupId > 0);
        Validate.notNull(status);

        try {
            return em.createNamedQuery(UserToGroup.QUERY_FIND_BY_GROUP_ID_AND_STATUS, UserToGroup.class)
                    .setParameter("groupId", groupId)
                    .setParameter("status", status)
                    .getResultList();
        } catch (NoResultException e) {
            return new ArrayList<UserToGroup>();
        }
    }

    public void remove(UserToGroup userToGroup) {
        Validate.notNull(userToGroup);

        em.remove(userToGroup);
    }
}
